﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using BtLabelModel;
using BtLabelTool;

namespace BtLabelBLL
{
    public class BtUartBLL
    {
        public SerialPort port;
        private RichTextBox _RichTextBoxInfo;
        public BtUartBLL(SerialPort port, RichTextBox RichTextBoxInfo)
        {
            this.port = port;
            _RichTextBoxInfo = RichTextBoxInfo;
        }
        /// <summary>
        /// 一发一收
        /// </summary>
        /// <param name="sendbyte">发送字节</param>
        /// <param name="delay">返回超时</param>
        /// <param name="wait">返回等待</param>
        /// <returns>返回字节</returns>
        public byte[] SendGetByte(byte[] sendbyte, int delay = 2000, int wait = 500)
        {
            try
            {
                //发送
                PortHelper.SendData(port, sendbyte);
                //写日志
                string senddata = DateTime.Now.ToString("HH:mm:ss.fff") + "\r\nSend:" + CommonHelper.GetByteStr(sendbyte);
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.SteelBlue, senddata);
                //读取返回
                byte[] recievebyte = PortHelper.GetByteData(port, delay, wait);
                //写日志
                string recievedata = DateTime.Now.ToString("HH:mm:ss.fff") + "\r\nRecieve:" + CommonHelper.GetByteStr(recievebyte); 
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.SteelBlue, recievedata);
                return recievebyte;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 搜索蓝牙设备
        /// </summary>
        /// <param name="delay">扫描等待</param>
        /// <param name="level">最小强度过滤</param>
        /// <returns></returns>
        public List<DeviceModel> GetBTDevices(byte delay, sbyte level)
        {
            try
            {
                List<DeviceModel> macs = new List<DeviceModel>();
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "设置扫描参数");
                byte[] recievebyte = SendGetByte(PortModel.BtUartModel.SetScan(delay, level));
                if (CommonHelper.EqualBytes(recievebyte, PortModel.BtUartModel.SetScanOk) == false)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "设置扫描参数失败");
                    return macs;
                }
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "设置扫描参数完成");

                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "扫描设备");
                recievebyte = SendGetByte(PortModel.BtUartModel.Scan);
                if (recievebyte.Length == 0)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "扫描设备返回超时");
                    return macs;
                }
                if (CommonHelper.EqualBytes(recievebyte, PortModel.BtUartModel.ScanOk) == false)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "扫描设备失败");
                    return macs;
                }
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "扫描等待" + (delay * 1000) / 1000 + "秒...");
                CommonHelper.Delay(delay * 1000);
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "查询扫描结果");
                recievebyte = SendGetByte(PortModel.BtUartModel.GetDevices);
                if (recievebyte.Length == 0)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "查询结果返回超时");
                    return macs;
                }
                if (recievebyte[0] != 0xA0 || recievebyte[1] != 0xE1)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "查询结果返回数据错误");
                    return macs;
                }
                if (recievebyte[2] * 256 + recievebyte[3] != recievebyte.Length - 4 || ((recievebyte.Length - 4) % 7) != 0)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "查询结果返回长度校验错误");
                    return macs;
                }
                if (recievebyte.Length == 4)
                {
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "未扫描到设备");
                    return macs;
                }
                UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.Blue, "解析扫描结果");
                List<string> list = new List<string>();
                int index = 1;
                for (int i = 4; i < recievebyte.Length - 4; i += 7)
                {
                    byte[] macbyte = new byte[] { recievebyte[i + 5], recievebyte[i + 4], recievebyte[i + 3], recievebyte[i + 2], recievebyte[i + 1], recievebyte[i] };
                    DeviceModel device = new DeviceModel()
                    {
                        Address = CommonHelper.GetByteStr(macbyte),
                        DBm = (sbyte)recievebyte[i + 6]
                    };
                    if (macs.Count(it => it.Address == device.Address) == 1)
                    {
                        continue;
                    }
                    macs.Add(device);
                    string item = index + " " + device.Address + " " + device.DBm;
                    index++;
                    list.Add(item);
                    UiBLL.RichTextBoxAppendText(_RichTextBoxInfo, Brushes.SteelBlue, item);
                }
                return macs;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
